package com.lanyou.esb.cook.proxy.entity;

import java.util.Date;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;

import com.lanyou.esb.cook.proxy.common.Constants;
import com.lanyou.esb.cook.proxy.meta.EnableType;

/**
 * 跟踪日志
 * 
 * @author Davey.wu
 */
public class TraceLog {
	/** 跟踪日志ID **/
	private String id;
	/** HTTP接口表ID **/
	private String ifId;
	/** 调用方系统编码 **/
	private String callSystemCode;
	/** 跟踪信息 **/
	private StringBuilder traceMsg;
	/** 是否可用 **/
	private String isEnable;

	/** 创建人 **/
	private String creator;

	/** 创建时间 **/
	private Date createdDate;

	/** 最后更新人员 **/
	private String modifier;

	/** 最后更新时间 **/
	private Date lastUpdatedDate;
	private int index = 1;
	private static int MAX_TRACE_MSG_LENGHT = 500;

	public static TraceLog getInstance() {
		Date now = new Date();
		TraceLog log = new TraceLog();
		log.createdDate = now;
		log.lastUpdatedDate = now;
		log.traceMsg = new StringBuilder();
		return log;
	}

	/**
	 * 添加跟踪信息
	 * 
	 * @param msg
	 */
	public void appendMsg(String msg) {
		this.traceMsg.append(index++ + ". " + msg + ";");
	}

	public TraceLog() {
		this.id = UUID.randomUUID().toString().replace("-", "");
		this.isEnable = EnableType.ENABLE.ValueCode;
		this.creator = Constants.DEFAULT_USER;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getIfId() {
		return ifId;
	}

	public void setIfId(String ifId) {
		this.ifId = ifId;
	}

	public String getCallSystemCode() {
		return callSystemCode;
	}

	public void setCallSystemCode(String callSystemCode) {
		this.callSystemCode = callSystemCode;
	}

	public String getTraceMsg() {
		String msg = traceMsg.toString();
		if (msg != null && msg.length() > MAX_TRACE_MSG_LENGHT) {
			msg = msg.substring(0, MAX_TRACE_MSG_LENGHT);
		}
		return msg;
	}

	public void setTraceMsg(String msg) {
		if (this.traceMsg == null) {
			this.traceMsg = new StringBuilder();
		}
		if (StringUtils.isBlank(msg)) {
			return;
		}
		this.traceMsg.append(msg);
	}

	public String getIsEnable() {
		return isEnable;
	}

	public void setIsEnable(String isEnable) {
		this.isEnable = isEnable;
	}

	public String getCreator() {
		return StringUtils.isEmpty(creator) ? Constants.DEFAULT_USER : creator;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	public Date getCreatedDate() {
		return createdDate == null ? new Date() : createdDate;
	}

	public void setCreatedDate(Date createdDate) {
		this.createdDate = createdDate;
	}

	public String getModifier() {
		return StringUtils.isEmpty(modifier) ? Constants.DEFAULT_USER
				: modifier;
	}

	public void setModifier(String modifier) {
		this.modifier = modifier;
	}

	public Date getLastUpdatedDate() {
		return lastUpdatedDate;
	}

	public void setLastUpdatedDate(Date lastUpdatedDate) {
		this.lastUpdatedDate = lastUpdatedDate;
	}

}
